package com.zmm.myyimixian.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zmm.myyimixian.MyApp;
import com.zmm.myyimixian.R;
import com.zmm.myyimixian.bean.FenLeiTitleItem;
import com.zmm.myyimixian.config.ConfigPath;
import com.zmm.myyimixian.db.ShoppingCart;
import com.zmm.myyimixian.util.UIManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wencai2015 on 2016/10/21.
 */

public class MyFeiLeiChildApater extends BaseAdapter {
    private List<ShoppingCart> mList;
    private List<FenLeiTitleItem.GoodsBean> goods;
    private ModifyCountInterface modifyCountInterface;
    private int count = 0;

    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }


    public void setShuJuKu(List<ShoppingCart> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void setData(List<FenLeiTitleItem.GoodsBean> goods) {
        this.goods = goods;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return goods.size();
    }

    @Override
    public Object getItem(int position) {
        return goods.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.boos_pull_listview, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final FenLeiTitleItem.GoodsBean goodsBean = goods.get(position);
        String promotion_img = goodsBean.getPromotion_img();
        if (!"".equals(promotion_img)) {
            Glide.with(parent.getContext()).load(ConfigPath.getLunBoPath(promotion_img)).placeholder(R.mipmap.icon_home_item_default).into(viewHolder.homebItemSmailIv);
        }
        Glide.with(parent.getContext()).load(ConfigPath.getLunBoPath(goodsBean.getImg())).placeholder(R.mipmap.icon_home_item_default).into(viewHolder.homebItemBigIv);
        String name = goodsBean.getName();
        viewHolder.homebNameTv.setText(name);
        String sub_name = goodsBean.getSub_name();
        if (sub_name == null || "".equals(sub_name)) {
            viewHolder.homebSubNameTv.setVisibility(View.GONE);
        } else {
            viewHolder.homebSubNameTv.setVisibility(View.VISIBLE);
            viewHolder.homebSubNameTv.setText(sub_name);
        }
        viewHolder.homebUnitDescTv.setText(goodsBean.getUnit_desc());
        viewHolder.homebItemRmbTv.setText("￥" + goodsBean.getUnit_price());
        final String count_zero_reason = goodsBean.getCount_zero_reason();
        //秒杀
        if (!"".equals(count_zero_reason)) {
            viewHolder.bossSjTv.setVisibility(View.VISIBLE);
            viewHolder.bossSjTv.setText(goodsBean.getSale_status());
            viewHolder.homebItemJiaIv.setImageResource(R.mipmap.icon_plus_gray);
        } else {
            viewHolder.bossSjTv.setVisibility(View.GONE);
            viewHolder.homebItemJiaIv.setImageResource(R.mipmap.icon_plus);
        }
        String is_buying_group = goodsBean.getIs_buying_group();
        //团购
        if ("1".equals(is_buying_group)) {
            viewHolder.bossJiajianLl.setVisibility(View.GONE);
            viewHolder.bossTgBtn.setVisibility(View.VISIBLE);
        } else {
            viewHolder.bossJiajianLl.setVisibility(View.VISIBLE);
            viewHolder.bossTgBtn.setVisibility(View.GONE);
            //查询数据跟json中的数据相比较控制控件的状态
            if (MyApp.isLogin() && MyApp.isSJK && mList != null) {
                for (int i = 0; i < mList.size(); i++) {
                    ShoppingCart shoppingCart = mList.get(i);
                    String title = shoppingCart.getTitle();
                    if (title.equals(name)) {
                        viewHolder.homebItemJianIv.setVisibility(View.VISIBLE);
                        viewHolder.homebItemCountTv.setVisibility(View.VISIBLE);
                        String count1 = shoppingCart.getCount();
                        this.count = Integer.valueOf(count1);
                        viewHolder.homebItemCountTv.setText(count1);
                        break;
                    } else {
                        viewHolder.homebItemCountTv.setText("0");
                        viewHolder.homebItemJianIv.setVisibility(View.GONE);
                        viewHolder.homebItemCountTv.setVisibility(View.GONE);
                    }
                }
            }
        }
        viewHolder.homebItemJiaIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!"".equals(count_zero_reason)) {
                    Toast.makeText(parent.getContext(), count_zero_reason, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (MyApp.isLogin()) {
                    modifyCountInterface.doIncrease(position, viewHolder.homebItemCountTv, viewHolder.homebItemJianIv, viewHolder.homebItemJiaIv, count);
                } else {
                    UIManager.startLoginActivity(parent.getContext());
                }
            }
        });
        viewHolder.homebItemJianIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doDecrease(position, viewHolder.homebItemCountTv, viewHolder.homebItemJianIv, viewHolder.homebItemJiaIv, count);
            }
        });

        viewHolder.bossTgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIManager.startGroupPurchase(parent.getContext(), goodsBean.getDesc_url());
            }
        });

        return convertView;
    }

    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param position      元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param count         数量
         * @param jian          减控件
         * @param jia           加控件
         */
        public void doIncrease(int position, TextView showCountView, ImageButton jian, ImageButton jia, int count);

        /**
         * 删减操作
         *
         * @param position      子元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param jian          减控件
         * @param jia           加控件
         * @param count         数量
         */
        public void doDecrease(int position, TextView showCountView, ImageButton jian, ImageButton jia, int count);
    }


    static class ViewHolder {
        @BindView(R.id.homeb_item_big_iv)
        ImageView homebItemBigIv;
        @BindView(R.id.homeb_name_tv)
        TextView homebNameTv;
        @BindView(R.id.homeb_sub_name_tv)
        TextView homebSubNameTv;
        @BindView(R.id.homeb_unit_desc_tv)
        TextView homebUnitDescTv;
        @BindView(R.id.homeb_item_jian_iv)
        ImageButton homebItemJianIv;
        @BindView(R.id.homeb_item_count_tv)
        TextView homebItemCountTv;
        @BindView(R.id.homeb_item_jia_iv)
        ImageButton homebItemJiaIv;
        @BindView(R.id.boss_jiajian_ll)
        LinearLayout bossJiajianLl;
        @BindView(R.id.boss_tg_btn)
        Button bossTgBtn;
        @BindView(R.id.homeb_item_smail_iv)
        ImageView homebItemSmailIv;
        @BindView(R.id.boss_sj_tv)
        TextView bossSjTv;
        @BindView(R.id.homeb_item_rmb_tv)
        TextView homebItemRmbTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
